require 'net/http'
require 'uri'

require './ticker.rb'
require './logger.rb'

class MtGox_Connector

  def MtGox_Connector.send_get_request(url)
    Logger.instance.log_with_timestamp("send_get_request: " + url.to_s)
    
    uri = URI.parse(url)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    
    resp, data = http.get(uri.path)
    
    Logger.instance.log_with_timestamp("response: " + resp.to_s)
    Logger.instance.log_with_timestamp("data: " + data.to_s)
    Logger.instance.log("")
    
    return data
  end

  def MtGox_Connector.send_post_request(url)
    Logger.instance.log_with_timestamp("send_post_request: " + url.to_s)
    
    uri = URI.parse(url)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE

    req_data = 'name=' + Settings.get_Login() + '&pass=' + Settings.get_Password()
    req_headers = {
      'Content-Type' => 'application/x-www-form-urlencoded'
    }
    Logger.instance.log("post data: " + req_data.to_s)

    resp, resp_data = http.post(uri.path, req_data, req_headers)
    
    Logger.instance.log_with_timestamp("response: " + resp.to_s)
    Logger.instance.log_with_timestamp("data: " + resp_data.to_s)
    Logger.instance.log("")

    return resp_data
  end

  def MtGox_Connector.send_post_request2(url, amount, price)
    Logger.instance.log_with_timestamp("send_post_request2: " + url.to_s)
    
    uri = URI.parse(url)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE

    req_data = 'name=' + Settings.get_Login() + '&pass=' + Settings.get_Password()
    req_data += "&amount=" + amount.to_s
    req_data += "&price=" + price.to_s
    req_headers = {
      'Content-Type' => 'application/x-www-form-urlencoded'
    }
    Logger.instance.log("post data: " + req_data.to_s)

    resp, resp_data = http.post(uri.path, req_data, req_headers)
    
    Logger.instance.log_with_timestamp("response: " + resp.to_s)
    Logger.instance.log_with_timestamp("data: " + resp_data.to_s)
    Logger.instance.log("")

    return resp_data
  end
  
  
  def MtGox_Connector.get_Ticker
    url = 'https://mtgox.com/code/data/ticker.php'
    json_ticker = MtGox_Connector.send_get_request(url)
    ticker = Ticker.parse_json(json_ticker)
    return ticker
  end

  def MtGox_Connector.get_Balance
    url = 'https://mtgox.com/code/getFunds.php'
    return MtGox_Connector.send_post_request(url)
  end

  def MtGox_Connector.get_Orders
    url = 'https://mtgox.com/code/getOrders.php'
    return MtGox_Connector.send_post_request(url)
  end
  
  def MtGox_Connector.buy_Market(amount, price)
    url = 'https://mtgox.com/code/buyBTC.php'
    return MtGox_Connector.send_post_request2(url, amount, price)
  end  

  def MtGox_Connector.sell_Market(amount, price)
    url = 'https://mtgox.com/code/sellBTC.php'
    return MtGox_Connector.send_post_request2(url, amount, price)
  end  
  
end